package com.szrt;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szrt.entity.UserEntity;

public class ElServlet extends HttpServlet {

	private static final long serialVersionUID = 7595828954275614124L;
	
	/**
	 * http://localhost:8080/mvn-department08/el?username=scott
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		
		//登录成功
		UserEntity userEntity = new UserEntity();
		userEntity.setUsername(username);
		
		//把UserEntity放入Cookie中
		//Cookie userCookie = new Cookie("userEntity",URLEncoder.encode(username, "utf-8"));
		//userCookie.setMaxAge(10*60);
		//response.addCookie(userCookie);
		
		
		//放入session中
		request.getSession().setAttribute("userEntity", userEntity);
		
		
		//做一些初始化的工作
		String[] arr= new String[]{"Tom","John","Sam","Ban","Bob"};
		request.setAttribute("arr", arr);
		
		List<String> list = Arrays.asList("Tom","John","Sam","Ban","Bob2");
		request.setAttribute("list", list);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("a", "11");
		map.put("b", "22");
		map.put("c", "33");
		map.put("d", "44");
		request.setAttribute("map", map);
		
		
		//作用域
		request.setAttribute("is", "a");
		request.getSession().setAttribute("is", "b");
		request.getSession().getServletContext().setAttribute("is", "c");
		
		//取web.xml中的初始化参数
		String company = request.getSession().getServletContext().getInitParameter("company");
		System.out.println("web.xml :"+company);
		
		
		int age=33;
		String password="xyz";
		
		String[] gongjiao={"B650","794"};
		//跳转到welcome页面
		request.getRequestDispatcher("el.jsp?age="+age+"&password="+password+"&gj="+gongjiao[0]+"&gj="+gongjiao[1]).forward(request, response);
		//response.sendRedirect(request.getContextPath()+"/welcome.jsp");
	}

}
